package com.joinway.framework.admin.core.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.joinway.framework.admin.bean.UserContext;
import com.joinway.framework.admin.bean.domain.AdminUser;
import com.joinway.framework.admin.bean.view.LoginRole;
import com.joinway.framework.admin.core.constant.AdminWebConstants;
import com.joinway.framework.core.plugin.PluginAdaptor;
import com.joinway.framework.data.jdbc.repository.TableRepository;

public class AbstractAdminService extends PluginAdaptor {

	@Autowired protected TableRepository repository;
	
	@Autowired protected AdminQueryService qservice;
	
	protected UserContext postLogin(AdminUser user, HttpServletRequest request) throws Exception {
		user.setLoginCount(user.getLoginCount() + 1);
		user.setLastLoginTime(new Date());
		repository.save(user);
		
		UserContext uc = new UserContext();
		uc.setUser(user);
		
		List<LoginRole> roles = qservice.findLoginRoles(user.getId());
		uc.setRoles(roles);
		
		request.getSession().setAttribute(AdminWebConstants.USER_CONTEXT_KEY, uc);
		
		return uc;
	}
}
